#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
key ::= row-key column-family column-qualifier flag timestamp
flag ::= DELETE_ROW | DELETE_COLUMN_FAMILY | DELETE_CELL | INSERT

DELETE_ROW           ::= 0x00
DELETE_COLUMN_FAMILY ::= 0x01
DELETE_CELL          ::= 0x02
INSERT               ::= 0xFF
'''

import time


ROW_KEY_MIN = '\x00\x00\x00\x00'
ROW_KEY_MAX = '\xFF\xFF\xFF\xFF'

#######################################################################

class   KeySmith(object) :
    ## Ascend Order
    #FLAG_DELETE_ROW           = 0x00
    #FLAG_DELETE_COLUMN_FAMILY = 0x01
    #FLAG_DELTE_CELL           = 0x02
    #FLAG_INSERT               = 0xFF

    ## Descend Order
    FLAG_DELETE_ROW           = 0xFF
    FLAG_DELETE_COLUMN_FAMILY = 0xFE
    FLAG_DELTE_CELL           = 0xFD
    FLAG_INSERT               = 0x00

    SEPARATOR = ' '

    def __init__(self, row_key=None, column_family=None, column_qualifier=None, flag=None, timestamp=None) :
        self.row_key = row_key
        self.column_family = column_family
        self.column_qualifier = column_qualifier
        self.flag = flag
        self.timestamp = timestamp

        if self.row_key :
            self.key = self.assemble()
        else :
            self.key = None
        # __init__()

    def assemble(self, row_key=None, column_family=None, column_qualifier=None, flag=None, timestamp=None) :
        if row_key          : self.row_key = row_key
        if column_family    : self.column_family = column_family
        if column_qualifier : self.column_qualifier = column_qualifier
        if flag             : self.flag = flag
        if timestamp        : self.timestamp = timestamp

        if self.column_family is None : self.column_family = ''
        if self.column_qualifier is None : self.column_qualifier = ''
        if self.flag is None : self.flag = self.FLAG_INSERT
        if self.timestamp is None : self.timestamp = int(time.time())

        row_key = self.row_key
        family = self.column_family
        qualifier = self.column_qualifier
        #timestamp  = hex(self.timestamp)
        timestamp  = '%X' % self.timestamp
        #flag = hex(self.flag)
        flag = '%02X' % self.flag

        key = self.SEPARATOR.join((row_key, family, qualifier, timestamp, flag))
        self.key = key
        return  key
        ## assemble()

    def desemble(self, key) :
        if key is None : return

        items = key.split(self.SEPARATOR, 4)
        self.row_key          = items[0]
        self.column_family    = items[1]
        self.column_qualifier = items[2]
        self.timestamp        = int(items[3], 16)
        self.flag             = int(items[4], 16)

        self._column_key = None
        self.key = key
        return True
        ## desemble()

    def isDeleteColumnFlag(self) :
        return self.FLAG_DELETE_COLUMN_FAMILY == self.flag
        ## isDeleteColumnFlag()

    def isDeleteRowFlag(self) :
        return self.FLAG_DELETE_ROW == self.flag
        ## isDeleteRowFlag()

    def getColumnKey(self, row_key=None, column_family=None, column_qualifier=None) :
        if row_key          is None : row_key = self.row_key
        if column_family    is None : column_family = self.column_family
        #if column_qualifier is None : column_qualifer = self.column_qualifier

        if row_key          is None : return None
        if column_family    is None : column_family = ''
        #if column_qualifier is None : column_qualifier = ''

        #column_key = self.SEPARATOR.join((row_key, column_family, column_qualifier))
        column_key = self.SEPARATOR.join((row_key, column_family))

        return column_key
        ## getColumnKey()

    def getColumnKeyRange(self, column_key=None) :
        '''
        @return (key_range_begin, key_range_end), where key_range_begin < key_range_end
        '''
        if not column_key : column_key = self.getColumnKey()
        if not column_key : return (None,None)

        range_begin_qualifier = '\x00'
        range_begin_timestamp = '0'
        range_begin_flag = '00'
        #range_begin = self.SEPARATOR.join((column_key, range_begin_timestamp, range_begin_flag))
        range_begin = self.SEPARATOR.join((column_key, range_begin_qualifier, range_begin_timestamp, range_begin_flag))

        range_end_qualifier = '\xFF'
        range_end_timestamp = 'FFFFFFFF'
        range_end_flag   = 'FF'
        #range_end = self.SEPARATOR.join((column_key, range_end_timestamp, range_end_flag))
        range_end = self.SEPARATOR.join((column_key, range_end_qualifier, range_end_timestamp, range_end_flag))

        return (range_begin, range_end)
        ## getKeyRange()

    def getCurrentTimestamp(self) :
        timestamp = int(time.time())

        return timestamp
        ## getCurrentTimestamp()

    # class KeySmith

#######################################################################

